#include "rtc_app.h"
#include "rtc_board.h"

static uint32_t RtcTimeCompensation = 0;

void RtcGetDeviceTime(TimeFormat_t *pDeviceTime)
{
  if (pDeviceTime != NULL)
  {
    // TimeFormat_t DeviceTime = {0};
    // RTC_GetTime(&DeviceTime.Year, &DeviceTime.Month, &DeviceTime.Date, &DeviceTime.Hour, &DeviceTime.Minute, &DeviceTime.Second);
    // uint32_t DeviceTimestamp = MakeTimestamp(&DeviceTime);

    // uint32_t ConversionsTimestamp = DeviceTimestamp + RtcTimeCompensation;
    // TimestampToNormalTime(ConversionsTimestamp, false, pDeviceTime);
    if (pDeviceTime != NULL)
    {
      uint32_t DeviceTimestamp = RTC_GetCounter() / 10000;
      uint32_t ConversionsTimestamp = DeviceTimestamp + RtcTimeCompensation;

      TimestampToNormalTime(ConversionsTimestamp, false,pDeviceTime);
    }
  }
}

bool RtcUpdateDeviceTime(uint32_t UpdateTimestamp)
{
  // TimeFormat_t DeviceTime = {0};

  // RTC_GetTime(&DeviceTime.Year, &DeviceTime.Month, &DeviceTime.Date, &DeviceTime.Hour, &DeviceTime.Minute, &DeviceTime.Second);
  // uint32_t DeviceTimestamp = MakeTimestamp(&DeviceTime);

  // if (UpdateTimestamp > DeviceTimestamp)
  // {
  //   RtcTimeCompensation = UpdateTimestamp - DeviceTimestamp;
  //   return true;
  // }

  // return false;
   uint32_t InternalTime = 0;

  if ((UpdateTimestamp != 0) && (UpdateTimestamp != 0xFFFFFFFF))
  {
    InternalTime = RTC_GetCounter() / 10000;
    RtcTimeCompensation = UpdateTimestamp - InternalTime;

    return true;
  }

  return false;
}
